﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using System.Configuration;
using System.Web.Configuration;

namespace AllNations.WebAppAdmin
{
    public partial class SiteSettings : MakeItWebAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btSaveSettings.Click += new EventHandler(btSaveSettings_Click);
            if (!IsPostBack)
            {
                txtSiteTitle.Text = ConfigurationManager.AppSettings["DefaultSiteTitle"];
                txtSiteDescription.Text = ConfigurationManager.AppSettings["DefaultSiteDescription"];
                txtSiteKeywords.Text = ConfigurationManager.AppSettings["DefaultSiteKeywords"];
                txtYouTubeDevAppName.Text = ConfigurationManager.AppSettings["YouTubeDevAppName"];
                imgBackgroundImage.ImageUrl = ConfigurationManager.AppSettings["BackgroundImage"];

            }
        }

        void btSaveSettings_Click(object sender, EventArgs e)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

            var appSettingsSection = config.GetSection("appSettings") as AppSettingsSection;
            if (appSettingsSection != null)
            {
                if (!appSettingsSection.Settings.AllKeys.Contains("DefaultSiteTitle"))
                    appSettingsSection.Settings.Add("DefaultSiteTitle", txtSiteTitle.Text.Trim());
                else
                    appSettingsSection.Settings["DefaultSiteTitle"].Value = txtSiteTitle.Text.Trim();

                if (!appSettingsSection.Settings.AllKeys.Contains("DefaultSiteDescription"))
                    appSettingsSection.Settings.Add("DefaultSiteDescription", txtSiteDescription.Text.Trim());
                else
                    appSettingsSection.Settings["DefaultSiteDescription"].Value = txtSiteDescription.Text.Trim();

                if (!appSettingsSection.Settings.AllKeys.Contains("DefaultSiteKeywords"))
                    appSettingsSection.Settings.Add("DefaultSiteKeywords", txtSiteKeywords.Text.Trim());
                else
                    appSettingsSection.Settings["DefaultSiteKeywords"].Value = txtSiteKeywords.Text.Trim();

                if (!appSettingsSection.Settings.AllKeys.Contains("YouTubeDevAppName"))
                    appSettingsSection.Settings.Add("YouTubeDevAppName", txtYouTubeDevAppName.Text.Trim());
                else
                    appSettingsSection.Settings["YouTubeDevAppName"].Value = txtYouTubeDevAppName.Text.Trim();

                if (fileUploadBackgroundImage.UploadedFiles.Count > 0)
                {
                    string sExt = fileUploadBackgroundImage.UploadedFiles[0].GetExtension();
                    Guid guidNewFileId = Guid.NewGuid();
                    string sImageFilename = string.Format("~/Images/bg/{0}{1}", guidNewFileId.ToString(), sExt);
                    string sFilename = Server.MapPath(sImageFilename);

                    fileUploadBackgroundImage.UploadedFiles[0].SaveAs(sFilename);

                    if (!appSettingsSection.Settings.AllKeys.Contains("BackgroundImage"))
                        appSettingsSection.Settings.Add("BackgroundImage", sImageFilename);
                    else
                        appSettingsSection.Settings["BackgroundImage"].Value = sImageFilename;

                    imgBackgroundImage.ImageUrl = sImageFilename;
                }
                config.Save(ConfigurationSaveMode.Minimal, true);
            }
        }
    }
}